﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Drawing.Drawing2D;
using System.Xml;
using System.IO;

namespace IWebApp2
{
    public partial class ConnectionSettings : Form
    {
        public GraphicsPath oPath;
        private bool IsFormBeingDragged = false;
        private int MouseDownX;
        private int MouseDownY;

        public delegate void ListBoxCallback(String str);
        private System.Xml.XmlTextWriter textWriter;
        private static string secure;
        private static string login;
        private static string proxy;


        public ConnectionSettings()
        {
            InitializeComponent();
            oPath = new GraphicsPath();
            Point[] myArray =
             {
                 new Point(0,9),
                 new Point(1,6),
                 new Point(3,4),
                 new Point(4,2),
                 new Point(6,0),    //upper left corner
               
                 new Point(277,0),
                 new Point(279,2),
                 new Point(281,4),
                 new Point(282,5),
                 //upper right corner
              
                 new Point(281,467),
                 new Point(279,469),
                 new Point(277,471),
                 new Point(275,472),
                 new Point(272,473),
                 //lower right corner
                
                 new Point(11,473),
                 new Point(8,472),
                 new Point(5,470),
                 new Point(2,467),
                 new Point(1,465),
                  //lower left corner*/

            };

            oPath.AddPolygon(myArray);
            this.Region = new Region(oPath);
            //this creates form with rounded corners
            StringBuilder sb = new StringBuilder();
            StreamReader re = File.OpenText("C:\\Documents and Settings\\Maricar Guingab\\My Documents\\Visual Studio 2008\\Projects\\IWebApp\\IWebApp2\\IWebApp2\\TextFile1.txt");

            string input = null;
            while ((input = re.ReadLine()) != null)
            {
                sb.Append(input);
            }

            this.txt_address.Text=GetElementInnerText(sb.ToString(),"Address");
            this.txt_port.Text = GetElementInnerText(sb.ToString(), "Port");
            secure=GetElementInnerText(sb.ToString(), "Option1");
            login = GetElementInnerText(sb.ToString(), "Option2");
            proxy = GetElementInnerText(sb.ToString(), "Option3");
            this.txt_proxy_address.Text = GetElementInnerText(sb.ToString(), "Proxy_Address");
            this.txt_proxy_port.Text = GetElementInnerText(sb.ToString(), "Proxy_Port");
   
            switch (Convert.ToInt32(secure))
            {
                case 1: radioSecure1.Select(); break;
                case 2: radioSecure2.Select(); break;
                case 3: radioSecure3.Select(); break;
                case 4: radioSecure4.Select(); break;
            }

            switch (Convert.ToInt32(login))
            {
                case 1: radioLogin1.Select(); break;
                case 2: radioLogin2.Select(); break;
            }

            switch (Convert.ToInt32(proxy))
            {
                case 1: radioProxy1.Select(); break;
                case 2: radioProxy2.Select(); break;
            }

            re.Close();
        }
 
        private void Update_ListBox(String str)
        {
      
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {

                this.Close();
                this.Dispose();
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "--" + ex.StackTrace.ToString());
            }
        }

        private void ConnectionSettings_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                IsFormBeingDragged = false;
            }
        }

        private void ConnectionSettings_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                IsFormBeingDragged = true;
                MouseDownX = e.X;
                MouseDownY = e.Y;
            }
        }

        private void ConnectionSettings_MouseMove(object sender, MouseEventArgs e)
        {
            if (IsFormBeingDragged)
            {
                Point temp = new Point();
                temp.X = this.Location.X + (e.X - MouseDownX);
                temp.Y = this.Location.Y + (e.Y - MouseDownY);
                this.Location = temp;
                temp.X = 0;
                temp.Y = 0;

            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
          
            this.WindowState = FormWindowState.Minimized;
        }

        public String GetElementInnerText(String XMLStringFormat, String text)
        {
            String stringNBtw = " ";
            String string1 = "<" + text + ">";
            String string2 = "</" + text + ">";
            if (XMLStringFormat.Contains(text))
            {
                int a = XMLStringFormat.IndexOf(string1);  //add 8 characters to advance to the sender name
                int b = XMLStringFormat.IndexOf(string2); //get the 
                stringNBtw = XMLStringFormat.Substring(a + text.Length + 2, b - (a + text.Length + 2));
            }
            return stringNBtw;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;
        }

        private void button4_Click(object sender, EventArgs e)
        {
           textWriter = new XmlTextWriter("C:\\Documents and Settings\\Maricar Guingab\\My Documents\\Visual Studio 2008\\Projects\\IWebApp\\IWebApp2\\IWebApp2\\TextFile1.txt", null);
           textWriter.Formatting = System.Xml.Formatting.Indented;
           textWriter.WriteStartDocument();
           textWriter.WriteStartElement("Data");

           textWriter.WriteStartElement("Address","");
           textWriter.WriteString(txt_address.Text);
           textWriter.WriteEndElement();

           textWriter.WriteStartElement("Port", "");
           textWriter.WriteString(txt_port.Text);
           textWriter.WriteEndElement();

           textWriter.WriteStartElement("Option1", "");
           textWriter.WriteString(secure.ToString());
           textWriter.WriteEndElement();

           textWriter.WriteStartElement("Option2", "");
           textWriter.WriteString(login.ToString());
           textWriter.WriteEndElement();

           textWriter.WriteStartElement("Option3", "");
           textWriter.WriteString(proxy.ToString());
           textWriter.WriteEndElement();

           if (proxy == "2")
           {
               textWriter.WriteStartElement("Proxy_Address", "");
               textWriter.WriteString(txt_proxy_address.Text);
               textWriter.WriteEndElement();

               textWriter.WriteStartElement("Proxy_Port", "");
               textWriter.WriteString(txt_proxy_port.Text);
               textWriter.WriteEndElement();
           }
            textWriter.WriteEndDocument();
            textWriter.Flush();
            textWriter.Close();


            //IWebApp2.SignIn.Port = txt_port.Text;
            //IWebApp2.SignIn.Address = txt_address.Text;

            this.Close();
            


           // Response.Redirect(Server.MapPath("PersonData.xml"))
        }

        private void button5_Click(object sender, EventArgs e)
        {
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }

    

        private void radioSecure1_CheckedChanged(object sender, EventArgs e)
        {
          
            secure = "1";
        }

        private void radioSecure3_CheckedChanged(object sender, EventArgs e)
        {
           
            secure = "3";
        }

        private void radioSecure2_CheckedChanged(object sender, EventArgs e)
        {
          
            secure = "2";
        }

        private void radioSecure4_CheckedChanged(object sender, EventArgs e)
        {
            secure = "4";
        }

        private void radioLogin1_CheckedChanged(object sender, EventArgs e)
        {
         
            login = "1";
        }

        private void radioLogin2_CheckedChanged(object sender, EventArgs e)
        {
          
            login = "2";
        }

        private void radioProxy1_CheckedChanged(object sender, EventArgs e)
        {
           
            proxy = "1";
        }

        private void radioProxy2_CheckedChanged(object sender, EventArgs e)
        {
           
            proxy = "2";
        }

        private void txt_address_TextChanged(object sender, EventArgs e)
        {
            this.txt_address.Font = new Font("Times New Roman",8,System.Drawing.FontStyle.Regular);
        }

      

    }
}